package edu.njfu.wigetdemo

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.LinearLayout
import androidx.recyclerview.widget.DividerItemDecoration
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import androidx.recyclerview.widget.StaggeredGridLayoutManager
import edu.njfu.wigetdemo.adapter.FruitAdapter2
import edu.njfu.wigetdemo.model.Fruit
import kotlinx.android.synthetic.main.activity_recycler_view.*
import java.lang.StringBuilder

class RecyclerViewActivity : AppCompatActivity() {
    //private val data=listOf("Apple","Orange","Pear","Grape")
    private  val fruitList=ArrayList<Fruit>()
    private fun initFruit(){
        repeat(2){
            fruitList.add(Fruit(getRandomLengthString("Apple"),R.drawable.apple_pic))
            fruitList.add(Fruit(getRandomLengthString("Banana"),R.drawable.banana_pic))
            fruitList.add(Fruit(getRandomLengthString("Orange"),R.drawable.orange_pic))
            fruitList.add(Fruit(getRandomLengthString("Watermelon"),R.drawable.watermelon_pic))
            fruitList.add(Fruit(getRandomLengthString("Pear"),R.drawable.pear_pic))
            fruitList.add(Fruit(getRandomLengthString("Grape"),R.drawable.grape_pic))
            fruitList.add(Fruit(getRandomLengthString("PineApple"),R.drawable.pineapple_pic))
            fruitList.add(Fruit(getRandomLengthString("StrawBerry"),R.drawable.strawberry_pic))
            fruitList.add(Fruit(getRandomLengthString("Cherry"),R.drawable.cherry_pic))
            fruitList.add(Fruit(getRandomLengthString("Mango"),R.drawable.mango_pic))
        }
    }
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_recycler_view)
        initFruit()
       // val layoutManger=LinearLayoutManager(this)
        val layoutManger=StaggeredGridLayoutManager(3,StaggeredGridLayoutManager.VERTICAL)
        //layoutManger.orientation=LinearLayoutManager.HORIZONTAL;
        recyclerView.layoutManager=layoutManger
        val adapter=FruitAdapter2(fruitList)
        recyclerView.adapter=FruitAdapter2(fruitList)

        recyclerView.addItemDecoration(DividerItemDecoration(this,DividerItemDecoration.VERTICAL))
       // recyclerView.ite

    }
    private fun getRandomLengthString(str:String):String{
        val n=(1..2).random()
        var builder=StringBuilder()
        repeat(n){
            builder.append(str)
        }
        return builder.toString()
    }

}

